package com.oracle.dao;

import com.oracle.dao.impl.FeedbackDao;
import com.oracle.mapper.FeedbackMapper;
import com.oracle.pojo.Feedback;
import com.oracle.pojo.FeedbackExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class FeedbackDaoImpl implements FeedbackDao {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Override
    public Integer addFeedBack(Feedback feedback) {
        int i = feedbackMapper.insertSelective(feedback);
        if (i>0){
            return 200;
        }
        return 400;
    }

    @Override
    public Feedback findFeedbackByOiid(Integer oiid) {
        try {
            FeedbackExample feedbackExample = new FeedbackExample();
            FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
            if (oiid!=null&&oiid!=0){
                criteria.andOiidEqualTo(oiid);
            }
            List<Feedback> feedbacks = feedbackMapper.selectByExampleWithBLOBs(feedbackExample);
            return feedbacks.isEmpty()?null:feedbacks.get(0);
        }catch (Exception e){
            return null;
        }

    }

    @Override
    public Integer updateFeedBack(Feedback feedback) {
        return feedbackMapper.updateByPrimaryKeySelective(feedback);
    }

    @Override
    public List<Feedback> findFeedBacksByUid(Integer uid) {
        try{
            FeedbackExample feedbackExample = new FeedbackExample();
            FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
            if (uid!=null&&uid!=0){
                criteria.andUidEqualTo(uid);
            }
            List<Feedback> feedbacks = feedbackMapper.selectByExampleWithBLOBs_new(feedbackExample);
            if (feedbacks.isEmpty()){
                return null;
            }
            return feedbacks;
        }catch (Exception e){
            return null;
        }

    }

    @Override
    public List<Feedback> findFeedbackByBid(Integer bid) {
        try{
            FeedbackExample feedbackExample = new FeedbackExample();
            FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
            if (bid!=null&&bid!=0){
                criteria.andBidEqualTo(bid);
            }
            List<Feedback> feedbacks = feedbackMapper.selectByExampleWithBLOBs_new(feedbackExample);
            if (feedbacks.isEmpty()){
                return null;
            }
            return feedbacks;
        }catch (Exception e){
            return null;
        }

    }
}
